package logic

import (
	"sync"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/models/statistics"
)

type TaskStatisticsLogic struct {
}

func NewTaskStatisticsLogic() *TaskStatisticsLogic {
	return &TaskStatisticsLogic{}
}

// 基础流水分析
func (t *TaskStatisticsLogic) StatisticsByDateStr(dateList []string) {
	for _, d := range dateList {
		var tempOrder, tempDividends bool
		var tempWg sync.WaitGroup
		tempWg.Add(2)
		go func() {
			defer tempWg.Done()
			tempOrder = dao.NewStatisticsDao().InsertOrderToTemp(d).Unwrap().(bool)
		}()
		go func() {
			defer tempWg.Done()
			tempDividends = dao.NewStatisticsDao().InsertDividendsToTemp(d).Unwrap().(bool)
		}()
		tempWg.Wait()

		if tempOrder && tempDividends {
			if ok := t.OrderAndIncomeStatistics(d); ok {
				dao.NewStatisticsDao().DeleteTempTable(d) // 清除临时表
			}
		}
	}
	//if len(dateList) <= 4 {
	//	for _, d := range dateList {
	//		if ok := dao.NewStatisticsDao().InsertOrderToTemp(d).Unwrap().(bool); ok { // 订单临时表
	//			if ok := dao.NewStatisticsDao().InsertDividendsToTemp(d).Unwrap().(bool); ok { // 分红临时表
	//				if ok := t.OrderAndIncomeStatistics(d); ok {
	//					dao.NewStatisticsDao().DeleteTempTable(d) // 清除临时表
	//				}
	//			}
	//		}
	//	}
	//} else {
	//	maxGoroutine := 4
	//	var pool chan struct{}
	//	pool = make(chan struct{}, maxGoroutine)
	//	var wg sync.WaitGroup
	//	for _, dateStr := range dateList {
	//		wg.Add(1)
	//		pool <- struct{}{}
	//		go func(d string) {
	//			defer wg.Done()
	//			defer func() {
	//				<- pool
	//			}()
	//			if ok := dao.NewStatisticsDao().InsertOrderToTemp(d).Unwrap().(bool); ok { // 订单临时表
	//				if ok := dao.NewStatisticsDao().InsertDividendsToTemp(d).Unwrap().(bool); ok { // 分红临时表
	//					if ok := t.OrderAndIncomeStatistics(d); ok {
	//						dao.NewStatisticsDao().DeleteTempTable(d) // 清除临时表
	//					}
	//				}
	//			}
	//		}(dateStr)
	//	}
	//	wg.Wait()
	//}
}

// 订单、收益统计
func (t *TaskStatisticsLogic) OrderAndIncomeStatistics(dateStr string) (ok bool) {
	m := &statistics.StatisticsModel{}
	scanData := &statistics.ScanChart{}
	var mEnd, mComplete, mCancel, mSy dao.CountSettlement
	var mPost, mdePost dao.CountSettlement
	var mWx, mZfb dao.CountSettlement
	var mAllDraw, mDraw, mNoDraw, mSmallDraw, mcNodraw, cNodraw dao.CountSettlement
	var wx, qrcode, qrcodeWait, qrcodeSuccess, qrcodeFail dao.CountSettlement
	var mCreate, mPlace, mDevice, incomeotal, incomeBusiness, incomeBranch, devices, places int64
	var w sync.WaitGroup
	w.Add(5)
	go func() {
		defer w.Done()
		mEnd = dao.NewStatisticsDao().QueryTotalOrderByState(0, dateStr).Unwrap().(dao.CountSettlement)
		mComplete = dao.NewStatisticsDao().QueryTotalOrderByState(5, dateStr).Unwrap().(dao.CountSettlement)
		mCancel = dao.NewStatisticsDao().QueryTotalOrderByState(3, dateStr).Unwrap().(dao.CountSettlement)
		mSy = dao.NewStatisticsDao().QueryTotalOrderByState(7, dateStr).Unwrap().(dao.CountSettlement)

		mCreate = dao.NewStatisticsDao().QueryTotalOrderByCreate(dateStr).Unwrap().(int64) // 创建时间订单总数

		mPost = dao.NewStatisticsDao().QueryTotalOrderByPayFrom(1, dateStr).Unwrap().(dao.CountSettlement) // 免押
		mdePost = dao.NewStatisticsDao().QueryTotalOrderByPayFrom(2, dateStr).Unwrap().(dao.CountSettlement) // 押金

		mWx = dao.NewStatisticsDao().QueryTotalOrderByOrderFrom("weixin", dateStr).Unwrap().(dao.CountSettlement) // 微信
		mZfb = dao.NewStatisticsDao().QueryTotalOrderByOrderFrom("zhifubao", dateStr).Unwrap().(dao.CountSettlement) // 支付宝
	}()
	go func() {
		defer w.Done()
		mPlace = dao.NewStatisticsDao().QueryTotalOrderGroupByPlaceId(dateStr).Unwrap().(int64) // 结束时间订单门店数-门店动销
		mDevice = dao.NewStatisticsDao().QueryTotalOrderGroupByDevice(dateStr).Unwrap().(int64) // 结束时间订单设备数 - 设备运销
		places = dao.NewStatisticsDao().QueryTotalPlaces(dateStr).Unwrap().(int64)
		devices = dao.NewStatisticsDao().QueryTotalDevices(dateStr).Unwrap().(int64)
	}()
	go func() {
		defer w.Done()
		mAllDraw = dao.NewStatisticsDao().QueryTotalOrderByDraw(4, 5, dateStr).Unwrap().(dao.CountSettlement)
		mDraw = dao.NewStatisticsDao().QueryTotalOrderByDraw(1, 5, dateStr).Unwrap().(dao.CountSettlement)
		mNoDraw = dao.NewStatisticsDao().QueryTotalOrderByDraw(2, 5, dateStr).Unwrap().(dao.CountSettlement)
		mSmallDraw = dao.NewStatisticsDao().QueryTotalOrderByDraw(3, 5, dateStr).Unwrap().(dao.CountSettlement)
		mcNodraw = dao.NewStatisticsDao().QueryTotalOrderByDraw(2, 5, dateStr).Unwrap().(dao.CountSettlement)
		cNodraw = dao.NewStatisticsDao().QueryTotalOrderByDraw(2, 3, dateStr).Unwrap().(dao.CountSettlement)
	}()
	go func() {
		defer w.Done()
		// 收益
		incomeotal = dao.NewStatisticsDao().QueryTotalIncomeByIsAdmin(0, dateStr).Unwrap().(int64)
		incomeBusiness = dao.NewStatisticsDao().QueryTotalIncomeByIsAdmin(helper.TypeNumIsAdminOperator, dateStr).Unwrap().(int64)
		incomeBranch = dao.NewStatisticsDao().QueryTotalIncomeByIsAdmin(helper.TypeNumIsAdminBranch, dateStr).Unwrap().(int64)

		// 提现
		wx = dao.NewStatisticsDao().QueryTotalCashLog(dateStr, 0, 0).Unwrap().(dao.CountSettlement)
		qrcode = dao.NewStatisticsDao().QueryTotalCashLog(dateStr, 2, 0).Unwrap().(dao.CountSettlement)
		qrcodeWait = dao.NewStatisticsDao().QueryTotalCashLog(dateStr, 2, 1).Unwrap().(dao.CountSettlement)
		qrcodeSuccess = dao.NewStatisticsDao().QueryTotalCashLog(dateStr, 2, 2).Unwrap().(dao.CountSettlement)
		qrcodeFail = dao.NewStatisticsDao().QueryTotalCashLog(dateStr, 2, 3).Unwrap().(dao.CountSettlement)
	}()
	go func() {
		defer w.Done()
		scanData = dao.NewStatisticsDao().GetScanPvByDateStr(dateStr).Unwrap().(*statistics.ScanChart)
	}()
	w.Wait()

	m.DateStr = dateStr

	m.CountEnd = mEnd.Total
	m.CountComplete = mComplete.Total
	m.CountCancel = mCancel.Total
	m.CountSy = mSy.Total
	m.CountCompleteNodraw = mcNodraw.Total
	m.CountCancelNodraw = cNodraw.Total

	m.SettlementTotal = mEnd.Money
	m.SettlementComplete = mComplete.Money
	m.SettlementCancel = mCancel.Money
	m.SettlementNormalTotal = mcNodraw.Money
	m.SettlementCancelNodraw = cNodraw.Money

	m.CountPost = mPost.Total
	m.CountDepost = mdePost.Total
	m.CountWx = mWx.Total
	m.CountZfb = mZfb.Total

	m.SettlementPost = mPost.Money
	m.SettlementDepost = mdePost.Money
	m.SettlementWx = mWx.Money
	m.SettlementZfb = mZfb.Money

	m.CountAlldraw = mAllDraw.Total
	m.CountDraw = mDraw.Total
	m.CountNodraw = mNoDraw.Total
	m.CountSmalldraw = mSmallDraw.Total

	m.SettlementAlldraw = mAllDraw.Money
	m.SettlementDraw = mDraw.Money
	m.SettlementNodraw = mNoDraw.Money
	m.SettlementSmalldraw = mSmallDraw.Money

	m.CountCreate = mCreate
	m.CountPlace = mPlace
	m.CountDevice = mDevice

	m.IncomeTotal = incomeotal
	m.IncomeBusiness = incomeBusiness
	m.IncomeBranch = incomeBranch

	m.WeixinTotal = wx.Total
	m.WeixinMoney = wx.Money
	m.QrcodeTotal = qrcode.Total
	m.QrcodeSuccessTotal = qrcodeSuccess.Total
	m.QrcodeSuccessMoney = qrcodeSuccess.Money
	m.QrcodeWaitTotal = qrcodeWait.Total
	m.QrcodeWaitMoney = qrcodeWait.Money
	m.CashFailedTotal = qrcodeFail.Total
	m.CashFailedMoney = qrcodeFail.Money

	m.Devices = devices
	m.Places = places

	m.ScanPv = scanData.PvCount
	m.ScanUv = scanData.UvCount

	ok = dao.NewStatisticsDao().InsertStatistics(m).Unwrap().(bool)
	return
}


